package com.aster.service.manage.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;

import com.aster.common.annotation.Log;
import com.aster.common.base.BaseController;
import com.aster.common.dto.UserToken;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.R;
import com.aster.service.manage.domain.TbEscortUserDuty;
import com.aster.service.manage.dto.DtoEscortUserDuty;
import com.aster.service.manage.dto.do2dto.EscortUserDutyConvert;
import com.aster.service.manage.service.EscortUserDutyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <br>
 * <b>功能：</b>TbEscortUserDutyController<br>
 *   <br>
 */ 
@RestController
@RequestMapping("/userDuty")
public class EscortUserDutyController extends BaseController {
	
	private final static Logger log= Logger.getLogger(EscortUserDutyController.class);
	@Autowired(required=false) 
	private EscortUserDutyService tbEscortUserDutyService;

	@Log("获取值班列表")
	@GetMapping("/getDutyList")
	public R getList(@RequestParam Map<String, Object> params){
		try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            TbEscortUserDuty tbEscortUserDuty = new TbEscortUserDuty();
			String escortUserId = (String) params.get("id");
			int id = Integer.parseInt(escortUserId);
			// 获取到当天的零时
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(new Date());
			calendar.set(Calendar.HOUR_OF_DAY, 0);
			calendar.set(Calendar.MINUTE, 0);
			calendar.set(Calendar.SECOND, 0);
            Date date = calendar.getTime();
            date = sdf.parse(sdf.format(date));

			tbEscortUserDuty.setEscortUserId(id);
			tbEscortUserDuty.setDutyTime(date);
			List<TbEscortUserDuty> list = tbEscortUserDutyService.getEscortUserDutyList(tbEscortUserDuty);
			List<TbEscortUserDuty> dutyList = new ArrayList<>();
			for (int i = 0; i< 30; i++){
				date = sdf.parse(sdf.format(calendar.getTime()));
				if (list != null && list.size() != 0){
					boolean flag = false; // 值班表是否有改天的数据标识
					for (TbEscortUserDuty userDuty:list) {
						Date dutyTime = sdf.parse(sdf.format(userDuty.getDutyTime()));
						if (dutyTime.equals(date)){
							dutyList.add(userDuty);
							flag = true;
							break;
						}
					}
					if (!flag){
						TbEscortUserDuty duty = new TbEscortUserDuty();
						duty.setEscortUserId(id);
						duty.setDutyTime(date);
						duty.setDutyState(0);
						dutyList.add(duty);
					}
				}else {
					// 如果该日期下没数据，则默认空闲状态
					TbEscortUserDuty duty = new TbEscortUserDuty();
					duty.setEscortUserId(id);
					duty.setDutyTime(date);
					duty.setDutyState(0);
					dutyList.add(duty);
				}
                // 往前加一天
				calendar.add(Calendar.DAY_OF_MONTH,1);
			}

//			// 循环取出30天的数据
//			for (int i = 0; i< 30; i++){
//				Date date = calendar.getTime();
//				tbEscortUserDuty.setDutyTime(date);
//				List<TbEscortUserDuty> list = tbEscortUserDutyService.queryByIdAndDutyTime(tbEscortUserDuty);
//				if (list != null && list.size() != 0){
//					dutyList.add(list.get(0));
//				}else {
//					// 如果该日期下没数据，则默认空闲状态
//					TbEscortUserDuty duty = new TbEscortUserDuty();
//					duty.setEscortUserId(id);
//					duty.setDutyTime(date);
//					duty.setDutyState(0);
//					dutyList.add(duty);
//				}
//				// 往前加一天
//				calendar.add(Calendar.DAY_OF_MONTH,1);
//			}
			return R.ok().put("info",dutyList);
		} catch (Exception e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}

	@Log("编辑值班信息")
	@PostMapping("/editDutyState")
	public R editState(@RequestBody TbEscortUserDuty tbEscortUserDuty,HttpServletRequest headers){
		try {
			UserToken userToken = getUserToken(headers);
			tbEscortUserDutyService.editDutyState(tbEscortUserDuty,userToken);
			return R.ok();
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}
}
